package com.ncu.weatherchina;

import android.os.Build;
import android.util.Xml;

import androidx.annotation.RequiresApi;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

public class RemoteDataParser {
    private static String WTHRCDN="http://wthrcdn.etouch.cn/WeatherApi?citykey=%s";
    private static String getRemoteData(String city_code){
        try {
            URL url = new URL(String.format(WTHRCDN,city_code));
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            StringBuffer result = new StringBuffer(1024);
            String tmp ="";
            while ((tmp=reader.readLine())!=null){
                result.append(tmp).append("\n");
            }
            reader.close();
            return result.toString();
        }catch (Exception e){
            return null;
        }
    }
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public static WeatherInfo parseRemoteData(String city_code){
        String xmlResult=getRemoteData(city_code);
        return PullParseXml(xmlResult);
    }
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    private static WeatherInfo PullParseXml(String xml){
        WeatherInfo weatherInfo =new WeatherInfo();
        InputStream inputStream = null;
        inputStream = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));

        XmlPullParser xmlPullParser = Xml.newPullParser();
        try {
            xmlPullParser.setInput(inputStream,"utf-8");
            WeatherForecast forecastInfo = null;
            WeatherIndex weatherIndex = null;
            boolean isDay=false;
            String text="";
            String tag="";

            int evtType=xmlPullParser.getEventType();
            while (evtType!=XmlPullParser.END_DOCUMENT){
                tag=xmlPullParser.getName();
                switch (evtType){
                    case XmlPullParser.START_TAG:
                        if (tag.equalsIgnoreCase("yesterday")){
                            forecastInfo = new WeatherForecast();
                        }
                        if (tag.equalsIgnoreCase("weather")){
                            forecastInfo = new WeatherForecast();
                        }
                        if(tag.equalsIgnoreCase("zhishu")){
                            weatherIndex = new WeatherIndex();
                        }
                        if (tag.equalsIgnoreCase("day_1")||tag.equalsIgnoreCase("day")){
                            isDay=true;
                        }
                        if (tag.equalsIgnoreCase("night_1")||tag.equalsIgnoreCase("night")){
                            isDay=false;
                        }
                        break;
                    case XmlPullParser.TEXT:
                        text=xmlPullParser.getText();
                        if (text==null)text="";
                        break;
                    case XmlPullParser.END_TAG:
                        if (tag.equalsIgnoreCase("city")){
                            weatherInfo.setCity(text);
                        }
                        if (tag.equalsIgnoreCase("updatetime")){
                            weatherInfo.setUpdate_time(text);
                        }
                        if (tag.equalsIgnoreCase("wendu")){
                            weatherInfo.setCurrent_temp(text);
                        }
                        if (tag.equalsIgnoreCase("fengli")&&forecastInfo==null){
                            weatherInfo.setCurrent_wind_force(text);
                        }
                        if (tag.equalsIgnoreCase("shidu")){
                            weatherInfo.setCurrent_humidity(text);
                        }
                        if (tag.equalsIgnoreCase("fengxiang")&&forecastInfo==null){
                            weatherInfo.setCurrent_wind_dir(text);
                        }
                        if (tag.equalsIgnoreCase("sunrise_1")){
                            weatherInfo.setSun_set(text);
                        }
                        if (tag.equalsIgnoreCase("sunset_1")){
                            weatherInfo.setSun_down(text);
                        }
                        if (tag.equalsIgnoreCase("aqi")){
                            weatherInfo.setCurrent_aqi(text);
                        }
                        if (tag.equalsIgnoreCase("pm25")){
                            weatherInfo.setCurrent_pm25(text);
                        }
                        if (tag.equalsIgnoreCase("quality")){
                            weatherInfo.setAir_quality(text);
                        }
                        if (tag.equalsIgnoreCase("date_1")&&forecastInfo!=null){
                            forecastInfo.setDate(text);
                        }
                        if (tag.equalsIgnoreCase("high_1")&&forecastInfo!=null){
                            forecastInfo.setTemp_max(text);
                        }
                        if (tag.equalsIgnoreCase("low_1")&&forecastInfo!=null){
                            forecastInfo.setTemp_min(text);
                        }
                        if (tag.equalsIgnoreCase("type_1")&& isDay &&forecastInfo!=null){
                            forecastInfo.setDay_type(text);
                        }
                        if (tag.equalsIgnoreCase("fx_1")&& isDay &&forecastInfo!=null){
                            forecastInfo.setDay_wind_dir(text);
                        }
                        if (tag.equalsIgnoreCase("fl_1")&& isDay &&forecastInfo!=null){
                            forecastInfo.setDay_wind_force(text);
                        }
                        if (tag.equalsIgnoreCase("type_1")&& !isDay &&forecastInfo!=null){
                            forecastInfo.setNight_type(text);
                        }
                        if (tag.equalsIgnoreCase("fx_1")&& !isDay &&forecastInfo!=null){
                            forecastInfo.setNight_wind_dir(text);
                        }
                        if (tag.equalsIgnoreCase("fl_1")&& !isDay &&forecastInfo!=null){
                            forecastInfo.setNight_wind_force(text);
                        }
                        if (tag.equalsIgnoreCase("date")&&forecastInfo!=null){
                            forecastInfo.setDate(text);
                        }
                        if (tag.equalsIgnoreCase("high")&&forecastInfo!=null){
                            forecastInfo.setTemp_max(text);
                        }
                        if (tag.equalsIgnoreCase("low")&&forecastInfo!=null){
                            forecastInfo.setTemp_min(text);
                        }
                        if (tag.equalsIgnoreCase("type")&& isDay &&forecastInfo!=null){
                            forecastInfo.setDay_type(text);
                        }
                        if (tag.equalsIgnoreCase("fengxiang")&& isDay &&forecastInfo!=null){
                            forecastInfo.setDay_wind_dir(text);
                        }
                        if (tag.equalsIgnoreCase("fengli")&& isDay &&forecastInfo!=null){
                            forecastInfo.setDay_wind_force(text);
                        }
                        if (tag.equalsIgnoreCase("type")&& !isDay &&forecastInfo!=null){
                            forecastInfo.setNight_type(text);
                        }
                        if (tag.equalsIgnoreCase("fengxiang")&& !isDay &&forecastInfo!=null){
                            forecastInfo.setNight_wind_dir(text);
                        }
                        if (tag.equalsIgnoreCase("fengli")&& !isDay &&forecastInfo!=null){
                            forecastInfo.setNight_wind_force(text);
                        }
                        if (tag.equalsIgnoreCase("yesterday")){
                            weatherInfo.setForecastInfo(forecastInfo);
                        }
                        if (tag.equalsIgnoreCase("weather")){
                            weatherInfo.setForecastInfo(forecastInfo);
                        }

                        if(tag.equalsIgnoreCase("name")){
                            weatherIndex.setName(text);
                        }
                        if(tag.equalsIgnoreCase("value")){
                            weatherIndex.setValue(text);
                        }
                        if(tag.equalsIgnoreCase("detail")){
                            weatherIndex.setDetail(text);
                        }
                        if(tag.equalsIgnoreCase("zhishu")){
                            weatherInfo.setIndexlist(weatherIndex);
                        }
                        break;
                    default:break;
                }
                evtType=xmlPullParser.next();
            }

        } catch (XmlPullParserException | IOException e) {
            e.printStackTrace();
        }
        return weatherInfo;
    }
}
